package dl.bukkit.myststus.function.condition.imp;

import dl.bukkit.myststus.configuration.imp.Config;
import dl.bukkit.myststus.function.condition.ICondition;
import dl.bukkit.myststus.hook.PlaceholderAPIHook;
import dl.bukkit.myststus.library.ats.GComponent;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.entity.Player;

@GComponent(
        key = "placeholder",
        author = "DogLong"
)
public class PlaceholderCondition implements ICondition {

    @Override
    public boolean isRun(Player player, String value) {
        if (value.contains(":")) {
            String[] split = value.split(":");
            String toString = PlaceholderAPIHook.toString(player, split[0]);
            if (NumberUtils.isNumber(toString)) {
                return NumberUtils.toDouble(toString) >= NumberUtils.toDouble(split[1]);
            }else {
                if (toString.lastIndexOf("%") == toString.length() - 1) {
                    String substring = toString.substring(0, toString.lastIndexOf("%"));
                    String substring1 = split[1].substring(0, split[1].lastIndexOf("%"));
                    return NumberUtils.toDouble(substring) >=  NumberUtils.toDouble(substring1);
                }else {
                    return toString.equalsIgnoreCase(split[1]);
                }
            }
        }
        return true;
    }

    @Override
    public boolean isRegister() {
        return Config.getInst().getBoolean("Condition.Placeholder");
    }
}
